<?php
defined('_JEXEC') or die( 'Restricted access' );
jimport( 'joomla.application.component.controller' ); 

class SanPhamController extends JController{ 
	function __construct($config = array()){
		parent::__construct($config);
		$this->registerTask( 'add',  'display' );
		$this->registerTask( 'edit', 'display' );
		$this->registerTask( 'publish', 'published' );
		$this->registerTask( 'unpublish', 'unpublished' );	
	}
	function display(){
		switch($this->getTask()){
			case 'add':
				JRequest::setVar( 'hidemainmenu', 0 );
				JRequest::setVar( 'layout', 'default'  );
				JRequest::setVar( 'view'  , 'detail');
				JRequest::setVar( 'edit', false );
			 	break;
			case 'edit':	
				JRequest::setVar( 'hidemainmenu', 0 );
				JRequest::setVar( 'layout', 'default'  );
				JRequest::setVar( 'view'  , 'detail');
				JRequest::setVar( 'edit', true );
				break;
			default:
				JRequest::setVar('view','list');
				JRequest::setVar('layout','default');
				break;
		}
		parent::display();
	}
	function save(){		
		$post = JRequest::get('post');					
		$model = $this->getModel('detail');
		if ($model->store($post)) {		
			$msg = JText::_( 'Lưu thành công' );				
			
		} else {
			$msg = JText::_( 'Có lỗi' );
		}
		if($post['saveColor']==1)
			$link = 'index.php?option=com_sanpham&view=chitiet&task=edit&cid[]='.$spid;
		else
			$link = 'index.php?option=com_sanpham';
		$this->setRedirect($link, $msg);
	}
	function published(){
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Bạn chưa chọn gi hết' ) );
		}
		$model = $this->getModel('detail');
		if(!$model->publish($cid,1)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_sanpham' );
	}
	
	function unpublished(){
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to publish' ) );
		}
		$model = $this->getModel('detail');
		if(!$model->publish($cid, 0)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_sanpham' );
	}
	function toggle_unSoldOut(){
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Bạn chưa chọn gi hết' ) );
		}
		$model = $this->getModel('detail');
		if(!$model->publishSoldOut($cid,0)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_sanpham' );
	}
	function toggle_SoldOut(){
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to publish' ) );
		}
		$model = $this->getModel('detail');
		if(!$model->publishSoldOut($cid, 1)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_sanpham' );
	}
	function remove(){
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Bạn chưa chọn gi hết' ) );
		}
		$model = $this->getModel('detail');
		if(!$model->delete($cid)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_sanpham','Đã xóa' );
	}
	function orderup(){
		$model = $this->getModel('detail');
		$model->move(-1);
		$this->setRedirect( 'index.php?option=com_sanpham');
	}
	function orderdown(){
		$model = $this->getModel('detail');
		$model->move(1);
		$this->setRedirect( 'index.php?option=com_sanpham');
	}
	function saveorder(){
		$cid 	= JRequest::getVar( 'cid', array(), 'post', 'array' );
		$order 	= JRequest::getVar( 'ordering', array(), 'post', 'array' );
		
		JArrayHelper::toInteger($cid);
		JArrayHelper::toInteger($order);

		$model = $this->getModel('detail');
		$model->saveorder($cid, $order);

		$msg = JText::_( 'New ordering saved' );
		$this->setRedirect( 'index.php?option=com_sanpham', $msg );
	}
	function toggle_frontpage(){
		global $mainframe,$option;
		$cid = JRequest::getVar('cid',array(0),'','array');
		$id = $cid[0];
		$db =&JFactory::getDBO();
		$query = 'select * from #__homepage where content_id = '.$id;	
		$db->setQuery($query);
		$row = $db->loadResult();
		if(count($row)){			
			$query = "DELETE FROM #__homepage WHERE content_id =".$id;	
			$db->setQuery( $query );
			if (!$db->query()) {
				echo "<script> alert('".$db->getErrorMsg()."');window.history.go(-1);</script>\n";
			}
		}else{
			$query = 'INSERT INTO #__homepage (content_id) VALUES ('.$id.')';
			$db->setQuery($query);
			$db->query();
			if ($db->getErrorNum()) {
      			echo $db->stderr();
      			return false;
    		}
		}
		$mainframe->redirect('index.php?option='.$option);
	}
	/*function toggle_newProduct(){
		global $mainframe,$option;
		$cid = JRequest::getVar('cid', array(0),'','array');
		$id = $cid[0];
		$db = &JFactory::getDBO();
		$query = "select * from #__newproduct where content_id = ".$id;
		$db->setQuery($query);
		$row= $db->loadResult();
		if(count($row)){
			$query = "delete from #__newproduct where content_id = ".$id;
			$db->setQuery($query);
			if(!$db->query()) {
				echo"<script> alert('".$db->getErrorMsg()."');window.history.go(-1);</script>\n";
			}
		}else{
			$query = "insert into #__newproduct (content_id) values(".$id.")";
			$db->setQuery($query);
			$db->query();
			if($db->getErrorNum()) {
				echo $db->stderr();
				return false;
			}
		}
		
		$mainframe->redirect('index.php?option='.$option);				 
		
	}*/
}